package com.samsung.controller.timer;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.samsung.common.annotation.LoginAjaxAuth;
import com.samsung.common.annotation.LoginAuth;
import com.samsung.common.util.BaseUtil;
import com.samsung.dto.timer.StopwatchDto;
import com.samsung.model.timer.TimerEvent;
import com.samsung.model.timer.TimerRecord;
import com.samsung.service.timer.StopwatchService;

/**
 * 	
 *	@ClassName: StopwatchController
 *	@Description: 秒表特效处理界面
 *	@author pu.huang
 *	@date 2015年5月14日 下午3:15:41
 *
 */
@Controller
@RequestMapping("/timer/stopwatch")
public class StopwatchController {

	@Resource
	private StopwatchService stopwatchService;
	
	/**
	 * 
	 * @Title: index
	 * @Description: particle秒表计时器首页
	 * @param @param request
	 * @param @param response
	 * @param @param model
	 * @param @return 设定文件
	 * @return String 返回类型
	 * @throws
	 */
	@LoginAuth
	@RequestMapping("/particle")
	public String particleIndex(ModelAndView model) {
		return "timer/particle-stopwatch";
	}
	
	/**
	 * 
	 * @Title: index
	 * @Description: simple秒表计时器首页
	 * @param @param request
	 * @param @param response
	 * @param @param model
	 * @param @return 设定文件
	 * @return String 返回类型
	 * @throws
	 */
	@LoginAuth
	@RequestMapping("/simple")
	public String simpleIndex(ModelAndView model) {
		return "timer/simple-stopwatch";
	}
	
//	@LoginAuth
//	@RequestMapping("/myResult")
//	public ModelAndView myResult(String uid, ModelAndView mav) {
//		StopwatchDto stopwatchDto = stopwatchService.getMyActivity(uid);
//		mav.addObject("stopwatchDto", stopwatchDto);
//		mav.setViewName("timer/myResult");
//		
//		return mav;
//	}
	
	/**
	 * 
	* @Title: save
	* @Description: 计时数据保存
	* @param @param mav
	* @param @return    设定文件
	* @return ModelAndView    返回类型
	* @throws
	 */
	@LoginAjaxAuth
	@ResponseBody
	@RequestMapping("/save")
	public Map<String, Object> save(@RequestBody StopwatchDto dto, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		String tid = BaseUtil.getUUID();
		TimerEvent event = new TimerEvent();
		event.setId(tid);
		event.setUid(BaseUtil.getLoginedUser(request).getUid());
		event.setTitle(dto.getTitle());
		event.setStartTime(new Date());
		event.setCreateTime(new Date());
		event.setType(dto.getType());
		stopwatchService.saveEvent(event);
		
		for (TimerRecord record : dto.getRecordList()) {
			record.setTid(tid);
			record.setId(BaseUtil.getUUID());
			stopwatchService.saveRecord(record);
		}
		
		map.put("success", true);
		map.put("msg", "计时活动保存成功");
		map.put("tid", tid);
		
		return map;
	}

}
